export const isEqual = (s, t) => {
    return s && s === t ? null : s;
}

/**
 * 验证身份证号
 * @param {*} idCard 
 * @returns 
 */
export const validIdCard = (idCard) => {
    if (!idCard || idCard.length < 18) {
        return false;
    }
    if (idCard.length === 18) {
        var idCard_string = JSON.stringify(idCard);
        var spstr = idCard_string.split("");
        var x = spstr[spstr.length - 2];
        console.log(spstr)
        if (x === "x") {
            console.log("身份证号最后一位X为大写");
            return false;
        }
        var reg = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
        // 检查生日日期是否正确，value就是身份证号
        var arrSplit = idCard.match(reg);
        if (!arrSplit) {
            console.log("身份证号格式有问题");
            return false;
        }
        var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
        var birthDay;
        birthDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && 
            ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]));
        if (!birthDay) {
            console.log("输入的身份证号里出生日期不对！", "error");
            return false;
        } else { 
            //检验18位身份证的校验码是否正确，校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            var valnum;
            let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
            var nTemp = 0, i;
            for (i = 0; i < 17; i++) {
                nTemp += idCard.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[nTemp % 11];
            if (valnum !== idCard.substr(17, 1)) {
                console.log("18位身份证的校验码不正确,应该为-:" + valnum);
                return false;
            }
            return true;
        }
    }
}

/**
 * 隐藏手机号
 * @param {*} mobile 
 * @returns 
 */
export const toMobile = (mobile) => {
    return mobile ? mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '';
}


/**
 * 验证邮箱
 * /^(.+)@(.+)(\.[a-zA-z0-9_-]+)$/
 * @param {*} email 
 * @returns 
 */
 export const validEmail = (email) => {
    return email ? /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email) : false;
}

/**
 * 隐藏身份证号
 * @param {*} idCard 
 * @returns 
 */
export const toIdCard = (idCard) => {
    return idCard ? idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2') : '';
}

/**
 * 验证电话
 * @param {*} value 
 * @returns 
 */
export const validPhone = (val) => {
    return val ? /^0\d{2,3}-\d{7,8}$/.test(val) : false;
}

export const isEmpty = v => !v || v === null || v === '';

export const isValidCode = v => v && v.length === 6; 

/**
 * 验证手机号
 * @param {*} value 
 * @returns 
 */
export const isMobile = (value) => value ? /^1[3|4|5|8|7|9][0-9]\d{8}$/.test(value) : false;

export const dealRes = (res) => {
    let data;
    if (res && ({data} = res)) {
        const { code, result } = data;
        if( code === 200 ) {
            return result;
        }
    }
    return null;
}


// 正则表达式校验正整数
export const isInt = (val) => {
    var regPos = /^[0-9]*[1-9][0-9]*$/; // 非负整数
    if (regPos.test(val)) {
      return true
    } else {
      return false
    }
}
export const isFloat = (val) => {
    var regPos = /^(\+)?\d+(\.\d+)?$/; // 非负整数
    if (regPos.test(val)) {
      return true
    } else {
      return false
    }
}

export const isFloat2 = (val) => {
    var regPos = /^(\+)?\d+(\.\d{1,2})?$/; // 非负整数
    if (regPos.test(val)) {
      return true
    } else {
      return false
    }
}
